package com.mrlee.controller;

import com.github.pagehelper.PageInfo;
import com.mrlee.bean.Admin;
import com.mrlee.bean.Role;
import com.mrlee.service.AdminService;
import com.mrlee.utils.AjaxResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by dllo on 18/3/12.
 */
@Controller
public class AdminController {

    @Resource
    private AdminService adminService;

    /**
     * 管理员列表页
     * @return
     */
    @RequestMapping(value = "adminlist")
    public String adminList(){
        return "admin/admin_list";
    }

    /**
     * 管理员添加页
     * @return
     */
    @RequestMapping(value = "adminadd")
    public String adminAdd(){
        return "admin/admin_add";
    }

    /**
     * 管理员更新页
     * @return
     */
    @RequestMapping(value = "adminupdate")
    public String adminUpdate(){
        return "admin/admin_modi";
    }

    /**
     * 查询全部管理员
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "alladmin")
    public AjaxResult selectAllAdmin(){
        return adminService.selectAllAdmin();
    }

    /**
     * 分页
     * @param pageNo 当前页
     * @param pageSize 每页显示信息数
     * @return
     */
    @RequestMapping(value = "adminpage")
    @ResponseBody
    public PageInfo<Admin> adminByPage(@RequestParam(value = "pagenum",required = false)Integer pageNo,
                                     @RequestParam(value = "pagesize",required = false)Integer pageSize){
        return adminService.queryAdminByPage(pageNo,3);
    }


    /**
     *添加管理员
     * @param name 姓名
     * @param code 登录名
     * @param password 密码
     * @param password1 确认密码
     * @param tel 电话
     * @param email 邮箱
     * @param roles 权限数组
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "addadmin")
    public AjaxResult addRole(@RequestParam("name")String name,
                              @RequestParam("code")String code,
                              @RequestParam("password")String password,
                              @RequestParam("password1")String password1,
                              @RequestParam("tel")String tel,
                              @RequestParam("email")String email,
                              @RequestParam("role[]") List<Integer> roles){
        AjaxResult ajaxResult = new AjaxResult();
        if ( name == null||code==null||password==null||password1==null||tel==null||email==null||roles==null) {
            ajaxResult.setCode("0");
            ajaxResult.setMessage("添加失败");
            return ajaxResult;
        }
        if (!password.equals(password1)){

            ajaxResult.setCode("0");
            ajaxResult.setMessage("密码不一致");
            return ajaxResult;
        }
        Admin admin = new Admin();
        admin.setName(name);
        admin.setAdminCode(code);
        admin.setPassword(password);
        admin.setTelephone(tel);
        admin.setEmail(email);

        return adminService.addAdmin(admin,roles);
    }

    /**
     * 删除管理员
     * @param adminId 管理员Id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "deladmin")
    public AjaxResult delAdmin(@RequestParam("adminId")int adminId){
        return adminService.deleteAdmin(adminId);
    }

    /**
     * 根据管理员Id查找管理员
     * @param adminId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "selectadmin")
    public Admin selectAdminById(@RequestParam("adminId")int adminId){
        return adminService.selectAdminById(adminId);
    }

    /**
     * 修改管理员信息
     * @param id 管理员Id
     * @param name 姓名
     * @param tel 电话
     * @param email 邮箱
     * @param roles 权限数组
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "updateadmin")
    public AjaxResult updateAdmin(@RequestParam("adminId")int id,
                                  @RequestParam("name")String name,
                                  @RequestParam("tel")String tel,
                                  @RequestParam("email")String email,
                                  @RequestParam("role[]") List<Integer> roles){
        AjaxResult ajaxResult = new AjaxResult();
        if ( name == null||tel==null||email==null||roles==null) {
            ajaxResult.setCode("0");
            ajaxResult.setMessage("添加失败");
            return ajaxResult;
        }

        Admin admin = new Admin();
        admin.setAdminId(id);
        admin.setName(name);
        admin.setTelephone(tel);
        admin.setEmail(email);
        return adminService.updateAdmin(admin,roles);
    }

    /**
     * 密码重置
     * @param管理员Id数组
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "resetpwd")
    public AjaxResult resetPwd(@RequestParam("admin[]") List<Integer> admins){
        return adminService.resetPwd(admins);
    }

    /**
     * 管理员搜索
     * @param roleName 角色
     * @param name 姓名
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "searchadmin")
    public AjaxResult search(@RequestParam("roleName") String roleName,@RequestParam("name") String name){
        return adminService.search(roleName,name);
    }
}
